/**
 * 
 */
package org.youisoft.summer.evaluator.function.clustering;

import java.util.List;

/**
 * @author zijiang.jl
 *
 */
public class KMedoidsFunction extends AbstractKSeriesFunction{

	@Override
	public String getName() {
		return "kmedoids";
	}

	@Override
	protected void adjustCenter(double[] center, List<Number> points,int[] centerIndex) {
		double[] pdist=new double[center.length];
		for(int i=0;i<pdist.length;i++){
			pdist[i]=-1;
		}
		
		double[] dist=new double[center.length];
		int[] idx=new int[center.length];
		
		for(int i=0;i<centerIndex.length;i++){
			
			for(int j=0;j<centerIndex.length;j++){
				
				//the same cluster
				if(centerIndex[i]==centerIndex[j]){
					
					dist[centerIndex[i]]+=java.lang.StrictMath.abs(points.get(j).doubleValue()-points.get(i).doubleValue());
				}
			}
			
			if(pdist[centerIndex[i]]==-1){
				pdist[centerIndex[i]]=dist[centerIndex[i]];
				idx[centerIndex[i]]=i;
			}else if(dist[centerIndex[i]]<pdist[centerIndex[i]]){
				pdist[centerIndex[i]]=dist[centerIndex[i]];
				idx[centerIndex[i]]=i;
			}
			dist[centerIndex[i]]=0;
		}
		
		for(int i=0;i<idx.length;i++){
			center[i]=points.get(idx[i]).doubleValue();
		}

	}

	
}
